package com.banach.prema.im.callback;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.banach.prema.im.annotation.Callback;
import com.banach.prema.im.annotation.CallbackCommand;
import com.banach.prema.repository.FriendRepository;
import com.banach.prema.repository.dataobject.FriendDO;
import com.ego.framework.jdbc.repository.JdbcRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
/**
 * 关系链系统回调服务
 * @author ajun
 *
 */
@Service
@Callback
public class SnsCallBackService {
	private final static CallBackResponse SUCCESS_RESPONSE = CallBackResponse.createSuccessResponse();;
	@Resource
	private FriendRepository friendRepository;
	@Resource
	private JdbcRepository jdbcRepository;
	private final static String DELETE_FRIEND_SQL = "delete from b_friend where user_id=? and friend_id=?";
	
	/**
	 *添加好友之后回调	
	 * @return
	 */
	@CallbackCommand("Sns.CallbackFriendAdd")
	public CallBackResponse friendAdd(JSONObject json){
		JSONArray pairList = json.getJSONArray("PairList");

		for(Object obj : pairList){
			JSONObject object = (JSONObject)obj;
			FriendDO friend = new FriendDO();
			friend.setCreateTime(new Date());
			friend.setUserId(object.getString("From_Account"));
			friend.setFriendId(object.getString("To_Account"));
			friendRepository.update(friend);
		}
		return SUCCESS_RESPONSE;
	}
	
	
	/**
	 *删除好友之后回调
	 * @return
	 */
	@CallbackCommand("Sns.CallbackFriendDelete")
	public CallBackResponse friendDelete(JSONObject json){
		JSONArray pairList = json.getJSONArray("PairList");

		for(Object obj : pairList){
			JSONObject object = (JSONObject)obj;
			jdbcRepository.update(DELETE_FRIEND_SQL, new Object[]{object.getString("From_Account"),object.getString("To_Account")});
		}
		return SUCCESS_RESPONSE;
	}

	
	/**
	 *添加黑名单之后回调
	 * @return
	 */
	@CallbackCommand("C2C.CallbackBlackListAdd")
	public CallBackResponse blackListAdd(JSONObject json){
		
		return SUCCESS_RESPONSE;
	}
	
	/**
	 *添加黑名单之后回调
	 * @return
	 */
	@CallbackCommand("C2C.CallbackBlackListDelete")
	public CallBackResponse blackListDelete(JSONObject json){
		
		return SUCCESS_RESPONSE;
	}

}
